package com.neuronbit.businessflow.intellij.ui.projectView;

import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiNamedElement;
import com.intellij.refactoring.RefactoringBundle;
import com.intellij.refactoring.rename.naming.AutomaticRenamer;
import com.neuronbit.businessflow.intellij.BpmBundle;
import com.neuronbit.businessflow.intellij.BpmFileType;
import com.neuronbit.businessflow.intellij.index.FlowClassIndex;

import java.util.List;

/**
 * The type Flows renamer.
 */
public class FlowsRenamer extends AutomaticRenamer {
    @Override
    public String nameToCanonicalName(String name, PsiNamedElement psiFile) {
        if (name.endsWith(BpmFileType.DOT_DEFAULT_EXTENSION)) return name.substring(0, name.length() - BpmFileType.DOT_DEFAULT_EXTENSION.length());
        return name;
    }

    @Override
    public String canonicalNameToName(String canonicalName, PsiNamedElement psiFile) {
        return canonicalName.contains(".") ? canonicalName : canonicalName + BpmFileType.DOT_DEFAULT_EXTENSION;
    }

    /**
     * Instantiates a new Flows renamer.
     *
     * @param aClass       the a class
     * @param newClassName the new class name
     */
    public FlowsRenamer(PsiClass aClass, String newClassName) {
        if (aClass.getQualifiedName() != null) {
            List<PsiFile> forms = FlowClassIndex.findFormsBoundToClass(aClass.getProject(), aClass);
            myElements.addAll(forms);
            suggestAllNames(aClass.getName(), newClassName);
        }
    }

    @Override
    public boolean isSelectedByDefault() {
        return true;
    }

    @Override
    public String getDialogTitle() {
        return BpmBundle.message("rename.bound.flows.title");
    }

    @Override
    public String getDialogDescription() {
        return RefactoringBundle.message("rename.forms.with.the.following.names.to");
    }

    @Override
    public String entityName() {
        return RefactoringBundle.message("entity.name.form");
    }
}
